﻿ /*
绘制图形
*/
import Math2;
class Draw 
{
	public static function DLine(tag:MovieClip,pData:Object,xc:Number,xs,alpha:Number)
	{
		if(tag==undefined)
		{
			trace("DrawLine:绘制目标不正确");
		}
		if(pData==undefined)
		{
			trace("DrawLine:绘制数据不正确");
			
		}
		tag.clear();
		tag.lineStyle(xc,xs,alpha)
		tag.moveTo(pData[0].x,pData[0].y);
			for(var i=1;i<pData.length;i++)
			{
				tag.lineTo(pData[i].x,pData[i].y);
			}
		
	}
	//---------------------- 绘制连续可控曲线 tag-绘制目标, pData-点数据, showP-是否显示线上点, PN-线上点linkage, showConP-是否显示控制点, CPN-控制点linkage, l-线粗, lc-线色, la-线透明度
	public static function DCurve (tag : MovieClip, pData : Object, showP : Boolean, PN : String, showConP : Boolean, CPN : String, l : Number, lc, la : Number) : Object 
	{
		if (tag == undefined)
		{
			trace ("DrawCurve:绘制目标不正确");
			return null;
		}
		if (pData == undefined || pData.point == undefined)
		{
			trace ("DrawCurve:绘制参数为空");
			return null;
		}
		if (l < 0 || l == undefined)
		{
			l = 0;
		}
		if (lc == undefined)
		{
		}
		lc = 0x000000;
		if (la == undefined || la < 0)
		{
			la = 100;
		}
		if (showP == undefined)
		{
			showP = true;
		}
		if (showConP == undefined)
		{
			showConP = true;
		}
		var d : Number = tag.getNextHighestDepth ();
		tag.createEmptyMovieClip ("C" + d.toString () , d);
		var ro : Object = new Object ();
		ro.l = l;
		ro.lc = lc;
		ro.la = la;
		ro.tag = tag ["C" + d.toString ()];
		ro.tag.obj = ro;
		ro.enableUpdata = true;
		ro.point = pData.point;
		ro.pTag = new Array ();
		//点mc组
		ro.cpTag = new Array ();
		//控制mc组
		//-------------------------- draw
		var cpa : Array = new Array ();
		//控制点array
		var i = 0;
		ro.tag.clear ();
		cpa [0] = {
			x : (pData.point [1].x + pData.point [0].x) / 2 + 5, y : (pData.point [1].y + pData.point [0].y) / 2 + 5
		};
		i ++;
		while (i < pData.point.length)
		{
			//trace("从("+pData.point[i-1].x+", "+pData.point[i-1].y+")画到("+pData.point[i].x+", "+pData.point[i].y+")，其中conp=("+cpa[(i-1)].x+", "+cpa[(i-1)].y+")");
			//--------------------------- 曲线
			ro.tag.lineStyle (l, lc, la);
			ro.tag.moveTo (pData.point [(i - 1)].x, pData.point [(i - 1)].y);
			cpa.push (
			{
				x : (2 * pData.point [i].x - cpa [(i - 1)].x) , y : (2 * pData.point [i].y - cpa [(i - 1)].y)
			});
			ro.tag.curveTo (cpa [(i - 1)].x, cpa [(i - 1)].y, pData.point [i].x, pData.point [i].y);
			//-------------------------------------------------------------------------------
			if (showP)
			{
				var pmc : MovieClip;
				var d = ro.tag.getNextHighestDepth ();
				if (PN == null || PN == "")
				{
					ro.tag.createEmptyMovieClip ("P" + d.toString () , d);
					Draw.DRound (ro.tag ["P" + d.toString ()] , 3, 0x0000ff);
					ro.tag ["P" + d.toString ()]._x = pData.point [(i - 1)].x;
					ro.tag ["P" + d.toString ()]._y = pData.point [(i - 1)].y;
					pmc = ro.tag ["P" + d.toString ()];
				} else 
				{
					ro.tag.attachMovie (PN, "P" + d.toString () , d);
					ro.tag ["P" + d.toString ()]._x = pData.point [(i - 1)].x;
					ro.tag ["P" + d.toString ()]._y = pData.point [(i - 1)].y;
					pmc = ro.tag ["P" + d.toString ()];
				}
				pmc.m = false;
				pmc.onPress = function ()
				{
					this.m = true;
				};
				pmc.onRelease = pmc.onReleaseOutside = function ()
				{
					this.m = false;
				};
				pmc.onEnterFrame = function ()
				{
					if (this.m)
					{
						var o : Object = {
							x : _root._xmouse, y : _root._ymouse
						};
						this._parent.globalToLocal (o);
						this._parent.obj.changePoint (this.i, o.x, o.y, true);
						this._x = o.x;
						this._y = o.y;
					}
				};
				pmc.i = (i - 1);
				ro.pTag.push (pmc);
			}
			//--------------------------- 控制线
			if (showConP)
			{
				ro.tag.lineStyle (0, 0x333333, 20);
				ro.tag.moveTo (pData.point [(i - 1)].x, pData.point [(i - 1)].y);
				ro.tag.lineTo (cpa [(i - 1)].x, cpa [(i - 1)].y);
				ro.tag.lineTo (pData.point [i].x, pData.point [i].y);
				var pmc : MovieClip;
				var d = ro.tag.getNextHighestDepth ();
				if (CPN == null || CPN == "")
				{
					ro.tag.createEmptyMovieClip ("CP" + d.toString () , d);
					Draw.DBox (ro.tag ["CP" + d.toString ()] , 4, 4, 0xff5522);
					ro.tag ["CP" + d.toString ()]._x = cpa [(i - 1)].x - 2;
					ro.tag ["CP" + d.toString ()]._y = cpa [(i - 1)].y - 2;
					pmc = ro.tag ["CP" + d.toString ()];
				} else 
				{
					ro.tag.attachMovie (CPN, "CP" + d.toString () , d);
					ro.tag ["CP" + d.toString ()]._x = cpa [(i - 1)].x - 2;
					ro.tag ["CP" + d.toString ()]._y = cpa [(i - 1)].y - 2;
					pmc = ro.tag ["CP" + d.toString ()];
				}
				pmc.m = false;
				pmc.onPress = function ()
				{
					this.m = true;
				};
				pmc.onRelease = pmc.onReleaseOutside = function ()
				{
					this.m = false;
				};
				pmc.onEnterFrame = function ()
				{
					if (this.m)
					{
						var o : Object = {
							x : _root._xmouse, y : _root._ymouse
						};
						this._parent.globalToLocal (o);
						this._parent.obj.changePoint (this.i, o.x, o.y, false);
					}
				};
				pmc.i = (i - 1);
				ro.cpTag.push (pmc);
			}
			i ++;
			//---------------最后一次画
			if (i >= pData.point.length)
			{
				if (showP)
				{
					var pmc : MovieClip;
					var d = ro.tag.getNextHighestDepth ();
					if (PN == null || PN == "")
					{
						ro.tag.createEmptyMovieClip ("P" + d.toString () , d);
						Draw.DRound (ro.tag ["P" + d.toString ()] , 3, 0x0000ff);
						ro.tag ["P" + d.toString ()]._x = pData.point [(i - 1)].x;
						ro.tag ["P" + d.toString ()]._y = pData.point [(i - 1)].y;
						pmc = ro.tag ["P" + d.toString ()];
					} else 
					{
						ro.tag.attachMovie (PN, "P" + d.toString () , d);
						ro.tag ["P" + d.toString ()]._x = pData.point [(i - 1)].x;
						ro.tag ["P" + d.toString ()]._y = pData.point [(i - 1)].y;
						pmc = ro.tag ["P" + d.toString ()];
					}
					pmc.m = false;
					pmc.onPress = function ()
					{
						this.m = true;
					};
					pmc.onRelease = pmc.onReleaseOutside = function ()
					{
						this.m = false;
					};
					pmc.onEnterFrame = function ()
					{
						if (this.m)
						{
							var o : Object = {
								x : _root._xmouse, y : _root._ymouse
							};
							this._parent.globalToLocal (o);
							this._parent.obj.changePoint (this.i, o.x, o.y, true);
							this._x = o.x;
							this._y = o.y;
						}
					};
					pmc.i = (i - 1);
					ro.pTag.push (pmc);
				}
				//--------------------------- 控制线
				if (showConP)
				{
					ro.tag.lineStyle (0, 0x333333, 20);
					ro.tag.moveTo (pData.point [(i - 1)].x, pData.point [(i - 1)].y);
					ro.tag.lineTo (cpa [(i - 1)].x, cpa [(i - 1)].y);
					var pmc : MovieClip;
					var d = ro.tag.getNextHighestDepth ();
					if (CPN == null || CPN == "")
					{
						ro.tag.createEmptyMovieClip ("CP" + d.toString () , d);
						Draw.DBox (ro.tag ["CP" + d.toString ()] , 4, 4, 0xff5522);
						ro.tag ["CP" + d.toString ()]._x = cpa [(i - 1)].x - 2;
						ro.tag ["CP" + d.toString ()]._y = cpa [(i - 1)].y - 2;
						pmc = ro.tag ["CP" + d.toString ()];
					} else 
					{
						ro.tag.attachMovie (CPN, "CP" + d.toString () , d);
						ro.tag ["CP" + d.toString ()]._x = cpa [(i - 1)].x - 2;
						ro.tag ["CP" + d.toString ()]._y = cpa [(i - 1)].y - 2;
						pmc = ro.tag ["CP" + d.toString ()];
					}
					pmc.m = false;
					pmc.onPress = function ()
					{
						this.m = true;
					};
					pmc.onRelease = pmc.onReleaseOutside = function ()
					{
						this.m = false;
					};
					pmc.onEnterFrame = function ()
					{
						if (this.m)
						{
							var o : Object = {
								x : _root._xmouse, y : _root._ymouse
							};
							this._parent.globalToLocal (o);
							this._parent.obj.changePoint (this.i, o.x, o.y, false);
						}
					};
					pmc.i = (i - 1);
					ro.cpTag.push (pmc);
				}
			}
		}
		ro.cpoint = cpa;
		//---------------------------------
		//Debug.traceAllObj(cpa);
		ro.Draw = function ()
		{
			this.tag.clear ();
			var i = 0;
			while (i < (this.point.length - 1))
			{
				this.tag.lineStyle (this.l, this.lc, this.la);
				this.tag.moveTo (this.point [i].x, this.point [i].y);
				this.tag.curveTo (this.cpoint [i].x, this.cpoint [i].y, this.point [(i + 1)].x, this.point [(i + 1)].y);
				if (this.enableUpdata)
				{
					this.pTag [i]._visible = true;
					this.cpTag [i]._visible = true;
					this.tag.lineStyle (0, 0x333333, 20);
					this.tag.moveTo (this.cpoint [i].x, this.cpoint [i].y);
					this.tag.lineTo (this.point [i].x, this.point [i].y);
					this.tag.moveTo (this.cpoint [i].x, this.cpoint [i].y);
					this.tag.lineTo (this.point [(i + 1)].x, this.point [(i + 1)].y);
				}else
				{
					this.pTag [i]._visible = false;
					this.cpTag [i]._visible = false;
				}
				i ++;
			}
		};
		ro.changePoint = function (i : Number, x : Number, y : Number, flag : Boolean)
		{
			if (flag)
			{
				//移动点
				//-------中间的点
				if (i > 0 && i < (this.point.length - 1))
				{
					var fp : Function = Math2.lineEquation (
					{
						x0 : this.point [(i - 1)].x, y0 : this.point [(i - 1)].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var f : Function = Math2.lineEquation (
					{
						x0 : this.point [i].x, y0 : this.point [i].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var fn : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 1)].x, y0 : this.point [(i + 1)].y, x1 : this.cpoint [i].x, y1 : this.cpoint [i].y
					});
					var nf : Function = Math2.lineEquation (
					{
						k : f ("k") , x2 : x, y2 : y
					});
					var np = {
						x : x, y : y
					};
					var ncp1 = Math2.Xpoint (
					{
						fun1 : nf, fun2 : fp
					});
					var ncp2 = Math2.Xpoint (
					{
						fun1 : nf, fun2 : fn
					});
					this.cpoint [(i - 1)] = ncp1;
					this.cpoint [i] = ncp2;
					this.point [i] = np;
					this.cpTag [i]._x = ncp2.x - 2;
					this.cpTag [i]._y = ncp2.y - 2;
					this.cpTag [(i - 1)]._x = ncp1.x - 2;
					this.cpTag [(i - 1)]._y = ncp1.y - 2;
				}
				//----头一点
				if (i == 0)
				{
					var f : Function = Math2.lineEquation (
					{
						x0 : this.point [0].x, y0 : this.point [0].y, x1 : this.cpoint [0].x, y1 : this.cpoint [0].y
					});
					var fn : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 1)].x, y0 : this.point [(i + 1)].y, x1 : this.cpoint [0].x, y1 : this.cpoint [0].y
					});
					var nf : Function = Math2.lineEquation (
					{
						k : f ("k") , x2 : x, y2 : y
					});
					var np = {
						x : x, y : y
					};
					var ncp = Math2.Xpoint (
					{
						fun1 : nf, fun2 : fn
					});
					this.cpoint [i] = ncp;
					this.point [i] = np;
					this.cpTag [i]._x = ncp.x - 2;
					this.cpTag [i]._y = ncp.y - 2;
				}
				//----结尾的点
				if (i == (this.point.length - 1))
				{
					var fp : Function = Math2.lineEquation (
					{
						x0 : this.point [(i - 1)].x, y0 : this.point [(i - 1)].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var f : Function = Math2.lineEquation (
					{
						x0 : this.point [i].x, y0 : this.point [i].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var nf : Function = Math2.lineEquation (
					{
						k : f ("k") , x2 : x, y2 : y
					});
					var np = {
						x : x, y : y
					};
					var ncp = Math2.Xpoint (
					{
						fun1 : nf, fun2 : fp
					});
					this.cpoint [(i - 1)] = ncp;
					var lx = np.x - this.point [i].x;
					var ly = np.y - this.point [i].y;
					var ncp2 = {
						x : (this.cpoint [i].x + lx) , y : (this.cpoint [i].y + ly)
					};
					this.cpoint [i] = ncp2;
					this.point [i] = np;
					this.cpTag [i]._x = ncp2.x - 2;
					this.cpTag [i]._y = ncp2.y - 2;
					this.cpTag [(i - 1)]._x = ncp.x - 2;
					this.cpTag [(i - 1)]._y = ncp.y - 2;
				}
			} else 
			{
				//移动控制点
				//--中间的点
				if (i > 0 && i < (this.cpoint.length - 2))
				{
					var fp : Function = Math2.lineEquation (
					{
						x0 : this.point [(i - 1)].x, y0 : this.point [(i - 1)].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var f1 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i)].x, y0 : this.point [(i)].y, x1 : x, y1 : y
					});
					var f2 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 1)].x, y0 : this.point [(i + 1)].y, x1 : x, y1 : y
					});
					var fn : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 2)].x, y0 : this.point [(i + 2)].y, x1 : this.cpoint [(i + 1)].x, y1 : this.cpoint [(i + 1)].y
					});
					if (f1 ("k") != fp ("k") && f2 ("k") != fn ("k"))
					{
						var ncp1 = Math2.Xpoint (
						{
							fun1 : fp, fun2 : f1
						});
						var ncp2 = Math2.Xpoint (
						{
							fun1 : fn, fun2 : f2
						});
						this.cpoint [i] = {
							x : x, y : y
						};
						this.cpoint [(i - 1)] = ncp1;
						this.cpoint [(i + 1)] = ncp2;
						this.cpTag [i]._x = x - 2;
						this.cpTag [i]._y = y - 2;
						this.cpTag [(i - 1)]._x = ncp1.x - 2;
						this.cpTag [(i - 1)]._y = ncp1.y - 2;
						this.cpTag [(i + 1)]._x = ncp2.x - 2;
						this.cpTag [(i + 1)]._y = ncp2.y - 2;
					}
				}
				//头上的点
				if (i == 0)
				{
					var f2 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 1)].x, y0 : this.point [(i + 1)].y, x1 : x, y1 : y
					});
					var fn : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 2)].x, y0 : this.point [(i + 2)].y, x1 : this.cpoint [(i + 1)].x, y1 : this.cpoint [(i + 1)].y
					});
					if (f2 ("k") != fn ("k"))
					{
						var ncp2 = Math2.Xpoint (
						{
							fun1 : fn, fun2 : f2
						});
						this.cpoint [i] = {
							x : x, y : y
						};
						this.cpoint [(i + 1)] = ncp2;
						this.cpTag [i]._x = x - 2;
						this.cpTag [i]._y = y - 2;
						this.cpTag [(i + 1)]._x = ncp2.x - 2;
						this.cpTag [(i + 1)]._y = ncp2.y - 2;
					}
				}
				//结尾的倒数第二点
				if (i == (this.cpoint.length - 2))
				{
					var fp : Function = Math2.lineEquation (
					{
						x0 : this.point [(i - 1)].x, y0 : this.point [(i - 1)].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var f1 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i)].x, y0 : this.point [(i)].y, x1 : x, y1 : y
					});
					var f2 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i + 1)].x, y0 : this.point [(i + 1)].y, x1 : x, y1 : y
					});
					if (f1 ("k") != fp ("k"))
					{
						var ncp1 = Math2.Xpoint (
						{
							fun1 : fp, fun2 : f1
						});
						var ncp2 = {
							x : f2 (null, this.point [(i + 1)].y) , y : this.point [(i + 1)].y
						};
						this.cpoint [i] = {
							x : x, y : y
						};
						this.cpoint [(i - 1)] = ncp1;
						this.cpoint [(i + 1)] = ncp2;
						this.cpTag [i]._x = x - 2;
						this.cpTag [i]._y = y - 2;
						this.cpTag [(i - 1)]._x = ncp1.x - 2;
						this.cpTag [(i - 1)]._y = ncp1.y - 2;
						this.cpTag [(i + 1)]._x = ncp2.x - 2;
						this.cpTag [(i + 1)]._y = ncp2.y - 2;
					}
				}
				//结尾的倒数第一点
				if (i == (this.cpoint.length - 1))
				{
					var fp : Function = Math2.lineEquation (
					{
						x0 : this.point [(i - 1)].x, y0 : this.point [(i - 1)].y, x1 : this.cpoint [(i - 1)].x, y1 : this.cpoint [(i - 1)].y
					});
					var f1 : Function = Math2.lineEquation (
					{
						x0 : this.point [(i)].x, y0 : this.point [(i)].y, x1 : x, y1 : y
					});
					if (f1 ("k") != fp ("k") && f1 ("k") != 0)
					{
						var ncp1 = Math2.Xpoint (
						{
							fun1 : fp, fun2 : f1
						});
						this.cpoint [i] = {
							x : x, y : y
						};
						this.cpoint [(i - 1)] = ncp1;
						this.cpTag [(i - 1)]._x = ncp1.x - 2;
						this.cpTag [(i - 1)]._y = ncp1.y - 2;
					}
					this.cpTag [i]._x = x - 2;
					this.cpTag [i]._y = y - 2;
				}
			}
			this.Draw ();
		};
		return ro;
	}
	//---------------------- 绘制圆形
	public static function DRound (tag : MovieClip, r : Number, fc, fa : Number, l : Number, lc, la : Number)
	{
		if (r <= 0 || r == undefined || tag == undefined)
		{
			trace ("DrawRound:参数错误。");
			return;
		}
		if (fc == undefined)
		{
			fc = 0xC4C4FF;
		}
		if (fa == undefined)
		{
			fa = 100;
		}
		if (l == undefined)
		{
			l = 0;
		}
		if (lc == undefined)
		{
			lc = 0xffffff;
		}
		if (la == undefined || la < 0)
		{
			la = 100;
		}
		if (l >= 0)
		{
			tag.lineStyle (l, lc, la);
		}
		tag.beginFill (fc, fa);
		tag.moveTo (0, - r);
		tag.curveTo (r, - r, r, 0);
		tag.curveTo (r, r, 0, r);
		tag.curveTo ( - r, r, - r, 0);
		tag.curveTo ( - r, - r, 0, - r);
		tag.endFill ();
	}
	//---------------------- 绘制矩形 tag-绘制目标, w-宽, h-高, fc-填充色, fa-透明度, l-线粗(小于零不画边线), lc-线色, la-线透明度
	public static function DBox (tag : MovieClip, w : Number, h : Number, fc, fa : Number, l : Number, lc, la : Number)
	{
		if (w <= 0 || w == undefined || h <= 0 || h == undefined || tag == undefined)
		{
			trace ("DrawBox:参数错误。");
			return;
		}
		if (fc == undefined)
		{
			fc = 0xC4C4FF;
		}
		if (fa == undefined)
		{
			fa = 100;
		}
		if (l == undefined)
		{
			l = 0;
		}
		if (lc == undefined)
		{
			lc = 0xffffff;
		}
		if (la == undefined || la < 0)
		{
			la = 100;
		}
		if (l >= 0)
		{
			tag.lineStyle (l, lc, la);
		}
		tag.beginFill (fc, fa);
		tag.moveTo (0, 0);
		tag.lineTo (w, 0);
		tag.lineTo (w, h);
		tag.lineTo (0, h);
		tag.lineTo (0, 0);
		tag.endFill ();
	}
	// ------------------------------------------------------------ help
	public static function HELP ()
	{
		var h : String = new String ();
		h += "-------------  HELP  --------------\r";
		h += "DCurve(tag:MovieClip, pData:Object, showP:Boolean, PN:String, showConP:Boolean, CPN:String, l:Number, lc, la:Number):Object\r";
		h += "绘制连续可控曲线 tag-绘制目标, pData-点数据, showP-是否显示线上点, PN-线上点linkage, showConP-是否显示控制点, CPN-控制点linkage, l-线粗, lc-线色, la-线透明度\r";
		h += "返回obj.tag为绘制区，obj.pTag为所有线上点mc的数据数组，obj.cpTag为所有控制点mc的数据数组，obj.point为所有线上点数据数组，obj.cpoint为所有控制点数据数组，obj.enableUpdata为控制点的显示开关（在下一次执行obj.Draw函数的时候起作用），obj.Draw()重绘图形\r";
		h += "\r";
		h += "DBox(tag:MovieClip, w:Number, h:Number, fc, fa:Number, l:Number, lc, la:Number)\r";
		h += "绘制矩形 tag-绘制目标, w-宽, h-高, fc-填充色, fa-透明度, l-线粗(小于零不画边线), lc-线色, la-线透明度\r";
		h += "\r";
		h += "DRound(tag:MovieClip, r:Number, fc, fa:Number, l:Number, lc, la:Number)\r";
		h += "绘制圆形  tag-绘制目标,r半径, fc-填充色, fa-透明度, l-线粗(小于零不画边线), lc-线色, la-线透明度\r";
		//h+="\r";
		//h+="\r";
		//h+="\r";
		h += "-------------  HELP  --------------\r";
		trace (h);
	};
}
